home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / OWLSRC.PAK / HSLIDER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.7 KB  |  231 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Implementation of THSlider, a horizontal slider UI widget
  8. //----------------------------------------------------------------------------
  9. #pragma hdrignore SECTION
  10. #include <owl/pch.h>
  11. #if !defined(OWL_SLIDER_H)
  12. # include <owl/slider.h>
  13. #endif
  14. #if !defined(OWL_DC_H)
  15. # include <owl/dc.h>
  16. #endif
  17. #if !defined(OWL_COMMCTRL_H)
  18. # include <owl/commctrl.h>
  19. #endif
  20. #if !defined(OWL_UIHELPER_H)
  21. # include <owl/uihelper.h>
  22. #endif
  23.  
  24. OWL_DIAGINFO;
  25.  
  26. #if !defined(SECTION) || SECTION == 1
  27.  
  28. //
  29. // Constructor for a THSlider object
  30. //
  31. THSlider::THSlider(TWindow*        parent,
  32.                    int             id,
  33.                    int x, int y, int w, int h,
  34.                    TResId          thumbResId,
  35.                    TModule*        module)
  36. :
  37.   TSlider(parent, id, x, y, w, h, thumbResId, module)
  38. {
  39.   if (!h)
  40.     Attr.H = 32;
  41.   Attr.Style |= TBS_HORZ;  // In case it is Native, else we dont care
  42. }
  43.  
  44. //
  45. // Constructor for a slider object created from resource
  46. //
  47. THSlider::THSlider(TWindow*        parent,
  48.                    int             resId,
  49.                    TResId          thumbResId,
  50.                    TModule*        module)
  51. :
  52.   TSlider(parent, resId, thumbResId, module)
  53. {
  54. }
  55.  
  56.  
  57. //----------------------------------------------------------------------------
  58. // Protected implementation
  59.  
  60. // Leave the whole implemenation out if built with OWL_NATIVECTRL_ALWAYS
  61. //
  62. #if !defined(OWL_NATIVECTRL_ALWAYS)
  63.  
  64. //
  65. // Calculate and return position given a thumb upper left point and vice versa.
  66. //
  67. int
  68. THSlider::PointToPos(const TPoint& point)
  69. {
  70.   int pixelRange = Attr.W - ThumbRect.Width() - 2;  // 2 is room for focus rect
  71.   long scaledX = long(point.x-1) * long(Range);
  72.   return int(scaledX/pixelRange + Min);
  73. }
  74.  
  75. //
  76. //
  77. //
  78. TPoint
  79. THSlider::PosToPoint(int pos)
  80. {
  81.   int pixelRange = Attr.W - ThumbRect.Width() - 2;  // 2 is room for focus rect
  82.   long relPos = pos - Min;
  83.   int  top = 1;
  84.   return TPoint(int((relPos*pixelRange)/Range)+1, top);
  85. }
  86.  
  87. //
  88. // Notify parent of a scroll event by sending a WM_HSCROLL message
  89. //
  90. void
  91. THSlider::NotifyParent(int scrollCode, int pos)
  92. {
  93. #if defined(BI_PLAT_WIN32)
  94.   Parent->HandleMessage(WM_HSCROLL, MkParam1(scrollCode, pos), TParam2(GetHandle()));
  95. #else
  96.   Parent->HandleMessage(WM_HSCROLL, scrollCode, MkParam2(pos, uint16(GetHandle())));
  97. #endif
  98. }
  99.  
  100. //
  101. // Determines if a point is within the thumb, or other hot areas of the
  102. // slider. Uses region if available, else uses thumb bounding rect.
  103. // Returns -1 if no hit.
  104. //
  105. int
  106. THSlider::HitTest(TPoint& point)
  107. {
  108.   if (ThumbRgn ? ThumbRgn->Contains(point) : ThumbRect.Contains(point))
  109.     return SB_THUMBTRACK;
  110.  
  111.   if (point.y > ThumbRect.bottom)
  112.     return SB_THUMBPOSITION;
  113.  
  114.   else if (point.x < ThumbRect.left)
  115.     return SB_PAGEUP;
  116.  
  117.   else if (point.x >= ThumbRect.right)
  118.     return SB_PAGEDOWN;
  119.  
  120.   return -1;
  121. }
  122.  
  123. //
  124. // Paint the ruler. The ruler doesn't overlap with the thumb or slot.
  125. // SysColors for text fg or bg are never dithered & can use TextRect.
  126. //
  127. void
  128. THSlider::PaintRuler(TDC& dc)
  129. {
  130.   const int ticH = 3;
  131.  
  132.   // Clear ruler area to bk color
  133.   //
  134.   dc.TextRect(0, ThumbRect.Height(), Attr.W, Attr.H, BkColor);
  135.  
  136.   // Draw left tic & internal tics if any, then right tic
  137.   //
  138.   int  margin = ThumbRect.Width()/2 + 1;
  139.   int  rulerY = ThumbRect.Height() + 2;
  140.   int  x;
  141.   int  h = ticH + 2;  // First & last are bigger
  142.  
  143.   dc.SetBkColor(TColor::SysBtnText);
  144.  
  145.   if (Tics && TicCount) {
  146.     for (int i = 0; i < TicCount; i++) {
  147.       x = PosToPoint(Tics[i]).x + margin;
  148.       dc.TextRect(x, rulerY, x+1, rulerY+h);
  149.       h = ticH;  // Middle ones are smaller
  150.     }
  151.   }
  152.   else {
  153.     for (int i = Min; i < Max; i += TicGap) {
  154.       x = PosToPoint(i).x + margin;
  155.       dc.TextRect(x, rulerY, x+1, rulerY+h);
  156.       if (!TicGap)
  157.         break;
  158.       h = ticH;  // Middle ones are smaller
  159.     }
  160.   }
  161.   x = Attr.W-margin-1;
  162.   dc.TextRect(x, rulerY, x+1, rulerY + ticH + 2);
  163. }
  164.  
  165. //
  166. // Paint the slot that the thumb slides over.
  167. //
  168. void
  169. THSlider::PaintSlot(TDC& dc)
  170. {
  171.   int    hmargin = ThumbRect.Width()/2 + 1;             // left & right margins
  172.   int    vmargin = (ThumbRect.Height()-SlotThick+1)/2+1;// top & bottom
  173.  
  174.   int    focusRectW = 1;  
  175.   int    focusRectH = 1;
  176.  
  177.   // Draw margins around slot in background color
  178.   //
  179.   dc.SetBkColor(BkColor);
  180.   // Top
  181.   dc.TextRect(focusRectW, focusRectH,
  182.               Attr.W-focusRectW, vmargin-focusRectH+SlotThick);
  183.   // Left
  184.   dc.TextRect(focusRectW, vmargin+focusRectH,
  185.               hmargin-focusRectW, vmargin+SlotThick-focusRectH);
  186.   // Right
  187.   dc.TextRect(Attr.W-hmargin+focusRectW, vmargin+focusRectH,
  188.               Attr.W-focusRectW, vmargin+SlotThick-focusRectH);
  189.   // Bottom
  190.   dc.TextRect(focusRectW, SlotThick+vmargin-focusRectH,
  191.               Attr.W-focusRectW, SlotThick+vmargin+vmargin-focusRectH);
  192.  
  193.   // Draw slot frame, shadow, fill & highlight below
  194.   //
  195.   TRect slotR(hmargin, vmargin, Attr.W-hmargin, vmargin + SlotThick);
  196.   TUIBorder b(slotR, TUIBorder::WndRecessed);
  197.   b.Paint(dc);
  198. }
  199.  
  200. #endif  // !defined(OWL_NATIVECTRL_ALWAYS)
  201.  
  202. #endif
  203. #if !defined(SECTION) || SECTION == 2
  204.  
  205. IMPLEMENT_STREAMABLE1(THSlider, TSlider);
  206.  
  207. #if !defined(BI_NO_OBJ_STREAMING)
  208.  
  209. //
  210. //
  211. //
  212. void*
  213. THSlider::Streamer::Read(ipstream& is, uint32 /*version*/) const
  214. {
  215.   ReadBaseObject((TSlider*)GetObject(), is);
  216.   return GetObject();
  217. }
  218.  
  219. //
  220. //
  221. //
  222. void
  223. THSlider::Streamer::Write(opstream& os) const
  224. {
  225.   WriteBaseObject((TSlider*)GetObject(), os);
  226. }
  227.  
  228. #endif  // if !defined(BI_NO_OBJ_STREAMING)
  229.  
  230. #endif
  231.